<?php

class CategoryController extends \BaseController {

    /**
     * 获得全部栏目名
     * @return [array] [栏目名，模型名，模型id]
     */
    public function index(){
        $categories = CategoryService::index();
        return View::make('category.index')->with('categories', $categories);
    }

    /**
     * 进入新增栏目页面
     * @return [array] [模型信息]
     */
    public function create(){
        $models = Model::all();
        return View::make('category.add')->with('models', $models);
    }

    /**
     * 增加栏目
     * @param [string] $categoryName    栏目名
     * @param [int] $modelId            所用模型id
     */
    public function add(){
        $categoryName  = Input::get('categoryName');
        $modelId       = Input::get('modelId');
        // $data        = Input::all();
        $data  = array('categoryName' => $categoryName, 'modelId' => $modelId);
        $rules = array('categoryName' => 'required | alpha_dash | unique:category,category_name',
                        'modelId'     => 'required | exists:model,id');
        $validator = CustomValidator::validateInput($data, $rules);
        if(isset($validator)){
            return Response::json($validator);
        }
        $message = CategoryService::add($categoryName, $modelId);
        return Response::json($message);
    }

    /**
     * 进入栏目更新字段
     * @return [array] [栏目id、栏目名，模型信息]
     */
    public function edit(){
        $categoryId = Input::get('categoryId');
        $data       = array('categoryId' => $categoryId);
        $rules      = array('categoryId' => 'required | exists:category,id');
        $validator  = CustomValidator::validateInput($data, $rules);
        if(isset($validator)){
            return Response::json($validator);
        }
        $category   = Category::find($categoryId);
        if(!isset($category)){
            return Response::json(CMSException::$noCategoryErr);
        }
        $models = Model::all();
        $data = array('category' => $category, 'models' => $models);
        return View::make('category.update')->with(array('category' => $category, 'models' => $models));
    }

    /**
     * 更新栏目
     * @return [array] [异常信息]
     */
    public function update(){
        $categoryName   = Input::get('categoryName');
        $modelId        = Input::get('modelId');
        $categoryId     = Input::get('categoryId');
        $data  = array('categoryName' => $categoryName, 'modelId' => $modelId, 'categoryId' => $categoryId);
        $rules = array('categoryName' => 'required',
                        'modelId'     => 'required | exists:model,id',
                        'categoryId'  => 'required | exists:category,id');
        $validator = CustomValidator::validateInput($data, $rules);
        if(isset($validator)){
            return Response::json($validator);
        }
        $category = array(
            'id'            => $categoryId,
            'categoryName'  => $categoryName,
            'modelId'       => $modelId);
        
        $message = CategoryService::update($category);
        return Response::json($message);
    }

    /**
     * 删除栏目
     * @return [array] [异常信息]
     */
    public function delete(){
        $categoryId = Input::get('categoryId');
        $data       = array('categoryId' => $categoryId);
        $rules      = array('categoryId' => 'required | exists:category,id');
        $validator  = CustomValidator::validateInput($data, $rules);
        if(isset($validator)){
            return Response::json($validator);
        }
        $message = CategoryService::delete($categoryId);
        return Response::json($message);
    }
}